package tuanda.soict.firstsun.PresentationPackage;

import tuanda.soict.firstsun.LogicPackage.Coordinate;

import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.math.Vector3;

public class AbstractTiledMapRendererScreen extends AbstractScreen implements
		GeneralConstants {

	protected TiledMapHelper tiledMapHelper;
	protected int lastTouchedX, lastTouchedY;
	protected int currentX, currentY, prevX, prevY;
	protected int screenWidth, screenHeight, mapWidth, mapHeight;
	protected int fixedWidth, fixedHeight, tapWidth, tapHeight;
	protected Coordinate coor;
	protected Camera camera;
	protected int rows, cols;
	protected int screenState;
	protected int generalState;
	protected long now, lastRender;
	protected String packerDirectory;
	protected String mapDirectory;

	public AbstractTiledMapRendererScreen(Object[] inputData,
			TalesOfTroubadour game) {
		super(inputData, game);
		screenState = generalState = DEFAULT;
		currentX = currentY = prevX = prevY = 0;
		setPackerDirectoryPath((String) data[0]);
		setMapDirectoryPath((String) data[1]);
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		setSize(width, height);
		initializeTileMapHelper();
	}

	@Override
	public void dispose() {
		super.dispose();
		tiledMapHelper.dispose();
	}

	public void controlFramePerSecond() {
		now = System.nanoTime();
		if (now - lastRender < 40000000) {
			try {
				Thread.sleep((40000000 - (now - lastRender)) / 1000000);
			} catch (InterruptedException e) {
			}
		}
		lastRender = now;
	}

	public void setSize(int sWidth, int sHeight) {
		if (sWidth > 0 && sHeight > 0) {
			this.screenWidth = sWidth;
			this.screenHeight = sHeight;
			fixedHeight = screenHeight >> 1;
			fixedWidth = screenWidth >> 1;
			tapWidth = fixedWidth >> 3;
			tapHeight = fixedHeight >> 3;
		}
	}

	public Coordinate convertCoordinate(int x, int y) {
		Vector3 temp = new Vector3(0, 0, 0);
		camera.unproject(temp);
		return new Coordinate((x + (int) temp.x), ((int) temp.y - y));
	}

	public TiledMapHelper getTiledMapHelper() {
		return this.tiledMapHelper;
	}

	public void checkCameraBoundaries() {
		if (camera.position.x < (fixedWidth)) {
			camera.position.x = fixedWidth;
		}
		if (camera.position.x >= (mapWidth - fixedWidth)) {
			camera.position.x = mapWidth - fixedWidth;
		}

		if (camera.position.y < (fixedHeight - 74)) {
			camera.position.y = fixedHeight - 74;
		}
		if (camera.position.y >= (mapHeight - fixedHeight)) {
			camera.position.y = mapHeight - fixedHeight;
		}
	}

	public void initializeTileMapHelper() {
		tiledMapHelper = new TiledMapHelper();
		tiledMapHelper.setPackerDirectory(getPackerDirectoryPath());
		tiledMapHelper.loadMap(getMapDirectoryPath());
		tiledMapHelper.prepareCamera(screenWidth, screenHeight);
		tiledMapHelper.create();
		camera = tiledMapHelper.getCamera();
		mapWidth = tiledMapHelper.getWidth();
		mapHeight = tiledMapHelper.getHeight();
	}

	public String getPackerDirectoryPath() {
		return this.packerDirectory;
	}

	public void setPackerDirectoryPath(String path) {
		if (path != null) {
			this.packerDirectory = path;
		}
	}

	public void setMapDirectoryPath(String path) {
		if (path != null) {
			this.mapDirectory = path + "/level.tmx";
		}
	}

	public String getMapDirectoryPath() {
		return this.mapDirectory;
	}

	public int getScreenState() {
		return this.screenState;
	}

	public void setScreenState(int s) {
		if (s > -1) {
			this.screenState = s;
		}
	}

	public int getCurrentX() {
		return this.currentX;
	}

	public int getCurrentY() {
		return this.currentY;
	}

	public int getPrevCurrentX() {
		return this.prevX;
	}

	public int getPrevCurrentY() {
		return this.prevY;
	}

	public int getGeneralState() {
		return generalState;
	}

	public void setGeneralState(int general) {
		this.generalState = general;
	}
}
